//
//  BarChartView.swift
//  Speek
//
//  Created by ZhenyanShan on 2023/6/25.
//
import SwiftUI

struct BarChartView: View {
    @Binding var dataPoints: [String]
    @Binding var dataValues: [Double]
    
    let cornerRadius: CGFloat = 8 // 圆角半径

    var body: some View {
        VStack {
            GeometryReader { geometry in
                HStack(spacing: 10) {
                    Spacer()
                    
                    ForEach(0..<dataPoints.count, id: \.self) { index in
                        
                        if !dataValues[index].isNaN {
                            VStack {
                                Spacer()
                                
                                RoundedRectangle(cornerRadius: cornerRadius) // 使用 RoundedRectangle 创建圆角矩形
                                    .fill(colors[index])
                                    .frame(width: (geometry.size.width / CGFloat(dataPoints.count) - 10)*0.5,
                                           height: calculateBarHeight(value: dataValues[index], maxHeight: calculateMaxBarHeight()),
                                           alignment: .bottom)
                                
                                Text(dataPoints[index])
                                    .font(.system(size: 12)) // 设置字体大小为12
                                //.frame(width: 40)
                                    .fixedSize(horizontal: true, vertical: false)
                                
                                Text(String(format: "%.1f%%", calculateBarPercentage(value: dataValues[index])))
                                    .font(.system(size: 10)) // 设置比例字体大小为10
                                    .fixedSize(horizontal: true, vertical: false)
                                    .padding(.bottom,5)
                            }
                        }
                    }
                    
                    Spacer()
                    
                }
                .padding(.bottom, geometry.size.height * 0.1)
                .alignmentGuide(.bottom) { _ in geometry.size.height } // 添加对齐指南，使柱状图底部对齐
                //.frame(alignment: .center)
            }
        }
    }

    private func calculateBarHeight(value: Double, maxHeight: CGFloat) -> CGFloat {
        let maxValue = dataValues.max() ?? 1.0
        let heightRatio = CGFloat(value / maxValue)
        return maxHeight * heightRatio
    }

    private func calculateMaxBarHeight() -> CGFloat {
        let maxHeight: CGFloat = 200 // 调整最大高度根据需要
        let maxRatio: CGFloat = 0.8 // 调整最大占比比例根据需要
        let maxValue = dataValues.max() ?? 1.0
        let heightRatio = CGFloat(maxValue) / CGFloat(maxRatio)
        return min(maxHeight * heightRatio, maxHeight)
    }

    private func calculateBarPercentage(value: Double) -> Double {
        let totalValue = dataValues.reduce(0, +)
        let percentage = (value / totalValue) * 100
        return percentage.isNaN ? 0 : percentage
    }
    
    //获取每个数据项
    private func getDataValues(tag: Bool, year: String, month: String) -> [Double] {
        guard let yearInt = Int(year) else { return [] } // 使用安全解包
        guard let monthInt = Int(month) else { return [] } // 使用安全解包
        let totalAmount = getTotalAmount(forYear: yearInt, month: monthInt, tag: tag)
    
        for index in 0..<dataPoints.count {
            let dataValue = getTotalAmount(forYear: yearInt, month: monthInt, tag: tag, selectedType: dataPoints[index]) / totalAmount
            dataValues.append(dataValue)
        }
        return dataValues
    }
}


